package drivers;

public class Config {
	public static final int INPUT_FILE_TYPE_CRAWL		= 0;
	public static final int INPUT_FILE_TYPE_PARSED		= 1;
	public static final int INPUT_FILE_TYPE_ANALYZED	= 2;

	public static final int DISPLAY_TYPE_NONE		= 0;
	public static final int DISPLAY_TYPE_ALL		= 1;
	public static final int DISPLAY_TYPE_ULTRAPEER	= 2;
	
	
	private static final String usage =
			"usage: Visigoth.java [-l numlines] [-p file]\n" +
			"					  [-fip ip] [-nt int] [-nu int] [-np int] [-ne int]\n" +
			"					  [-cf color] [-cu color] [-cp color] [-cl color] [-i [u]]\n" +
			"						[-n [u]]\n" +
			"					  [-h] inputfile\n" +
			"	Parsing options:\n\n" +
			"		[-l int]	number of lines from sourcelogfile to parse\n" +
			"		[-p file]	file to save serialized parsed data structure to (increases\n" +
			"					speed of future runs)\n" +
			"	Analysis options:\n" +
			"		[-fip ip]	focuse IP address to being analysis at\n" +
			"		[-nt int]	max total number of nodes\n" +
			"		[-nu int]	max number of ultrapeers\n" +
			"		[-np int]	max number of peers\n" +
			"		[-ne int]	max number of edges\n" +
			"	Display options:'\n" +
			"		[-cf color]	color (RGB triplet or HTML color name) of focus ultrapeer node\n" +
			"		[-cu color]	color (RGB triplet or HTML color name) of ultrapeers\n" +
			"		[-cp color]	color (RGB triplet or HTML color name) of peers\n" +
			"		[-cl color]	color (RGB triplet or HTML color name) of leaves\n" +
			"		[-ce color]	color (RGB triplet or HTML color name) of edges\n" +
			"		[-i [u]] 	display IP address with nodes. optionally only display IPs\n" +
			"					of ultrapeers.\n" +
			"		[-n [u]]	display number of connections. optionally only display\n" +
			"					number of connections for ultrapeers\n" +
			"	General options:\n" +
			"		[-h]		show this usage message\n" +
			"		inputfile	location of raw crawl log\n " +
			"";
	
	// Parsing options
	private int		numLines = -1;				// Number of lines from sourceLogFile to parse
	
	// Analysis options
	private int		maxNodesTotal = 1000;			// Max total number of nodes
	private int		maxNumUltrapeers = 1000;		// Max number of ultrapeer nodes
	private int		maxNumPeers = 1000;			// Max number of peer nodes
	private int		maxNumEdges = 1000;			// Maximum total number of edges
	
	// Display options
	private String 	focusIPAddress = "";		// IP address to focus the analysis on
	private String	colorFocusNode = "white";	// Color of the focus ultrapeer node
	private String	colorUltrapeers = "red";	// Color of ultrapeer nodes
	private String	colorPeers = "orange";		// Color of peer nodes
	private String	colorLeaves = "blue";		// Color of leaf nodes
	private String	colorEdges = "yellow";		// Color of edges
	private int		displayIPs = DISPLAY_TYPE_NONE;		// Display the IP address next to each node
	private int		displayNumConn = DISPLAY_TYPE_NONE;	// Display the number of connections next to each node
	
	// General options
	private String	inputFile = "../dat/crawl.log";				// Input file
	
	public boolean parseArgumentsArr(String[] args) {
		boolean success = true;
		
		String curOpt = "";
		// Loop through each option and parse them one by one
		// It is assumed that all options have one argument (i=i+2), so comenate for
		//	this manually if the option only has one argument (by subtracting one from i before
		//	the next iteration).
		
		for (int i = 0; i < args.length; i=i+2) {
							
			curOpt = args[i];
				
			//allows for no parameters
			if (curOpt.equals(""))
				return true;
			
			// If any errors occur, just print the usage again.
			try {
				// Parsing options
				if(curOpt.equals("-l")) {
					numLines = Integer.parseInt(args[i+1]);
				
				// Analysis options
				} else if(curOpt.equals("-fip")) {
					focusIPAddress = args[i+1];
				} else if(curOpt.equals("-nt")) {
					maxNodesTotal = Integer.parseInt(args[i+1]);
				} else if(curOpt.equals("-nu")) {
					maxNumUltrapeers = Integer.parseInt(args[i+1]);
				} else if(curOpt.equals("-np")) {
					maxNumPeers = Integer.parseInt(args[i+1]);
				} else if(curOpt.equals("-ne")) {
					maxNumEdges = Integer.parseInt(args[i+1]);
				
				// Display options
				} else if(curOpt.equals("-cf")) {
					colorFocusNode = args[i+1];
				} else if(curOpt.equals("-cu")) {
					colorUltrapeers = args[i+1];
				} else if(curOpt.equals("-cp")) {
					colorPeers = args[i+1];
				} else if(curOpt.equals("-cl")) {
					colorLeaves = args[i+1];
				} else if(curOpt.equals("-ce")) {
					colorEdges = args[i+1];
				}  else if(curOpt.equals("-i")) {
					if(args[i+1].equals("u")) {
						displayIPs = DISPLAY_TYPE_ULTRAPEER;
					} else {
						displayIPs = DISPLAY_TYPE_ALL;
						i = i - 1;		// Compensate for lack of argument for this option
					}
				} else if(curOpt.equals("-n")) {
					if(args[i+1].equals("u")) {
						displayNumConn = DISPLAY_TYPE_ULTRAPEER;
					} else {
						displayNumConn = DISPLAY_TYPE_ALL;
						i = i - 1;		// Compensate for lack of argument for this option
					}
					
				// General options
				} else if(curOpt.equals("-h")) {
					System.out.println(Config.usage);
					System.exit(1);
				} else if(!curOpt.substring(0, 1).equals("-")) {
					// If the first char isn't a dash, then we are dealing with the inputfile
					inputFile = curOpt;
				}
			} catch (Exception e) {
				System.out.println(Config.usage);
				success = false;
			}
		}	
		
		return success;
	}
	
	public String getFocusIPAddress() {
		return focusIPAddress;
	}

	public String getColorEdges() {
		return colorEdges;
	}

	public String getColorFocusNode() {
		return colorFocusNode;
	}

	public String getColorLeaves() {
		return colorLeaves;
	}

	public String getColorPeers() {
		return colorPeers;
	}

	public String getColorUltrapeers() {
		return colorUltrapeers;
	}

	public int getDisplayIPs() {
		return displayIPs;
	}

	public int getDisplayNumConn() {
		return displayNumConn;
	}

	public String getInputFile() {
		return inputFile;
	}

	public int getMaxNumEdges() {
		return maxNumEdges;
	}

	public int getMaxNumPeers() {
		return maxNumPeers;
	}

	public int getMaxNumUltrapeers() {
		return maxNumUltrapeers;
	}

	public int getNumLines() {
		return numLines;
	}

	public int getMaxNodesTotal() {
		return maxNodesTotal;
	}

	public String getUsage() {
		return usage;
	}
}
